from ..utils.crypto_utils import crypto_utils
from ..utils.crypto_utils import server_utils
from ..utils.config_reader import read_util
from ..utils.logger import logger
from src.backend.backend_user import backend_user
from src.backend.backend_course import backend_course
from .app_base import AppBase
from datetime import datetime

auth_data = read_util.load_assertions('app_auth_data.yaml')
plan_push_data = read_util.load_assertions('app_study_plan_data.yaml')


class AppStudyPlan:
    """
    学习计划模块服务类，提供与学习计划相关的各种操作。
    """

    def push_plan(self):
        """
        测试步骤推送每日学习计划，auth_data是认证用例，plan_data是学习计划用例，user_id是通过认证用例的手机号查询出来的值，now_time是算出当天日期的值，并将这两个值赋值给学习计划用例中的
        userInfoId、planPeriodStart字段
        """
        try:
            user_id = backend_user.get_user_info_id(auth_data['App登录测试用例']['请求数据']['loginName'])
            now_time = datetime.now().strftime('%Y-%m-%d')

            plan_push_data['pushStudyPlan']['request_data']['selectedUserList'][0]['userInfoId'] = user_id
            plan_push_data['pushStudyPlan']['request_data']['selectedUserList'][0]['planPeriodStart'] = now_time

            self.push_study_plan(plan_push_data['pushStudyPlan'])
        except Exception as e:
            logger.error(f"推送学习计划出错: {e}", exc_info=True)

    @staticmethod
    @server_utils.handle_exceptions("学习计划-查询计划列表失败")
    def find_task_list(plan_data):
        """
        学习计划-查询列表
        """
        return crypto_utils.app_post_request(url=f"/v1/userInfoStudyPlan/findTaskList", data=crypto_utils.prepare_data(plan_data))

    @staticmethod
    @server_utils.handle_exceptions("学习计划-推送计划失败")
    def push_study_plan(plan_data):
        """
        学习计划-推送
        """
        server_utils.post_request("/uc/v1/userInfoStudyPlanTpl/referenceSave", json_data=plan_data['request_data'])

    @staticmethod
    @server_utils.handle_exceptions("学习计划-查询计划详情失败")
    def get_task_detail(plan_data):
        """
        学习计划-查询计划详情
        """
        return crypto_utils.app_post_request(url=f"/v1/userInfoStudyPlan/getTaskDetail", data=crypto_utils.prepare_data(plan_data))

    @staticmethod
    @server_utils.handle_exceptions("学习计划-查询纸上课堂详情失败")
    def get_paperCourse_detail(plan_data):
        """
        学习计划-查询纸上课堂详情
        """
        return crypto_utils.app_post_request(url=f"/v1/paperCourse/detail", data=crypto_utils.prepare_data(plan_data))

    @staticmethod
    @server_utils.handle_exceptions("学习计划-查询计划预览失败")
    def plan_preview(plan_data):
        """
        学习计划-查询计划预览
        """
        return crypto_utils.app_post_request(url=f"/v1/userInfoStudyPlan/planPreview", data=crypto_utils.prepare_data(plan_data))

    @staticmethod
    @server_utils.handle_exceptions("学习计划-查询计划列表失败")
    def plan_myStudyPlanList(plan_data):
        """
        学习计划-查询计划列表
        """
        return crypto_utils.app_post_request(url=f"/v1/userInfoStudyPlan/myStudyPlanList", data=crypto_utils.prepare_data(plan_data))

    @staticmethod
    @server_utils.handle_exceptions("学习计划-导出课表计划失败")
    def plan_Export(plan_data):
        """
        学习计划-导出课表计划
        """
        return crypto_utils.app_post_request(url=f"/v1/userInfoStudyPlan/export", data=crypto_utils.prepare_data(plan_data))


study_plan = AppStudyPlan()
